package b2.widget.guide
{
	import b2.config.*;
	import b2.net.*;
	import b2.scene.base.*;
	import b2.scene.guide.*;
	import b2.widget.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class TankSelectDialog extends Sprite
	{
		//坦克列表
		private var tankList:Array;
		//当前显示的坦克
		private var currentTankIndex:int;
		
		public function TankSelectDialog()
		{
			super();
			var MessageBoxGgr:Class = ResourceHelper.getInstance().getClassByName("TankSelectBgr");
			var mainSprite:Sprite = new MessageBoxGgr();
			addChild(mainSprite);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 28, 0x000000);
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "请选择角色";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 60;
			addChild(titleText);
			//坦克列表
			tankList = new Array();
			for (var i:int = 0; i < Config.basicConfig.guideTank.length; i++)
			{
				var tankItem:TankSelectItem = new TankSelectItem(Config.basicConfig.guideTank[i]);
				tankItem.x = 330;
				tankItem.y = 160;
				tankList.push(tankItem);
				addChild(tankItem);
			}
			currentTankIndex = Math.random() * tankList.length;
			for (i = 0; i < tankList.length; i++)
			{
				if (i != currentTankIndex)
				{
					tankList[i].visible = false;
				}
			}
			//左箭头
			var LeftArrow:Class = ResourceHelper.getInstance().getClassByName("LeftArrow1");
			var leftArrow:BasicButton = new BasicButton(LeftArrow);
			leftArrow.addEventListener(MouseEvent.CLICK, onLeftArrowMouseClick);
			leftArrow.x = 268;
			leftArrow.y = 210;
			addChild(leftArrow);
			//右箭头
			var RightArrow:Class = ResourceHelper.getInstance().getClassByName("RightArrow1");
			var rightArrow:BasicButton = new BasicButton(RightArrow);
			rightArrow.addEventListener(MouseEvent.CLICK, onRightArrowMouseClick);
			rightArrow.x = 498;
			rightArrow.y = 210;
			addChild(rightArrow);
			//确定按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr7");
			var buttonOK:BasicButton = new BasicButton(ButtonBgr, "确定", 20);
			buttonOK.x = (this.width  - buttonOK.width) / 2;
			buttonOK.y = 350;
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			addChild(buttonOK);
		}
		
		private function setShow(index:int):void
		{
			tankList[currentTankIndex].visible = false;
			tankList[index].visible = true;
			currentTankIndex = index;
		}
		
		private function onLeftArrowMouseClick(event:MouseEvent):void
		{
			var index:int = (tankList.length + currentTankIndex - 1) % tankList.length; 
			setShow(index);
		}
		
		private function onRightArrowMouseClick(event:MouseEvent):void
		{
			var index:int = (currentTankIndex + 1) % tankList.length; 
			setShow(index);
		}

		private function onButtonOKClick(event:MouseEvent):void
		{
			close();
			ServerHelper.getInstance().selectTank(Config.basicConfig.guideTank[currentTankIndex]);
			ServerHelper.getInstance().addEventListener(ServerEvent.SELECT_TANK, onSelectTankComplete);
		}
		
		private function onSelectTankComplete(event:ServerEvent):void
		{
			BaseScene.getInstance().playerPicIcon.refresh();
			GuideScene.getInstance().step++;
		}
		
		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
        
	}
}